<template>
    <view class="login">
        <view class="logo_img">
            <image src="https://gatherabc.oss-cn-qingdao.aliyuncs.com/xcx/logo_index.png" alt=""></image>
        </view>
        <view class="log_button">
            <view @tap="loginByWX">
                <view><i class="iconfont icon-weixin"></i></view>
                <button open-type="getUserInfo" id="wxLogin">微信注册</button>
            </view>
            <view @tap="registerBtn">
                <view><i class="iconfont icon-iphone"></i></view>
                <text>手机注册</text>
            </view>
        </view>
    </view>
</template>
<script>
    import wepy from 'wepy';
    import api from '@/api/api';
    import utils from '@/utils/utils';
    import tip from '@/utils/tip';
    import {
        USER_INFO, USER_SPECICAL_INFO,USER_ID
    } from '@/utils/constant';

    export default class login extends wepy.page {
        config = {  // 配置当前页面

            navigationBarTitleText: '登录'    // 头部标题
        };
        components = {};

        data = {

        };

        methods = {
            loginByWX() {
                wx.login({
                  success: function(resone) {
                    wx.getUserInfo({
                        success: function (restwo) {
                            console.log("$$$$$",restwo.encryptedData.split('+').join('\+'))
                            // 获取授权code后登录
                            api.wxRegister({
                                query:{}
                            },'code='+resone.code).then(function(resthree) {
                                let open_id = resthree.data.data;
                                api.wxRegisterToo({
                                    query: {}
                                },'openId='+open_id+'&encryptedData='+restwo.encryptedData.split('+').join('%2B')+"&iv="+restwo.iv).then(function(resfour){
                                    if(resfour.data.code == "0"){
                                        tip.toast("微信注册成功");
                                        wepy.setStorageSync("userId",resfour.data.data.userId);
                                        // 返回手机号为空则去绑定手机号页面，否则跳转登录页面
                                        if (!resfour.data.data.phone) {
                                            wepy.navigateTo({
                                                url: '/pages/personalCenter/wxBindPhone?userid='+resfour.data.data.userId
                                            });
                                        }else{
                                            wepy.navigateTo({
                                                url: '/pages/personalCenter/login'
                                            }); 
                                        }
                                        
                                    }else{
                                        tip.error(resthree.data.msg)
                                    }
                                })
                            })
                        },
                        fail:function(err) {
                            tip.error('获取用户信息失败')
                        }
                    })
                  },
                  fail:function(err) {
                    console.log("####00########00",err)
                    tip.error('失败')
                  }
                })
            },
            registerBtn() {
                wepy.navigateTo({
                    url: '/pages/personalCenter/phoneRegister'
                });
            }
        };
        events = {};
    }

</script>
<style lang="less">
    .login {
        background: #fff;
        height: 100vh;
        .logo_img {
            width: ~'196rpx';
            height: ~'331rpx';
            margin: 0 auto;
            padding-top: ~'200rpx';
            padding-bottom: ~'200rpx';
            image {
                width: ~"196rpx";
                height: ~"331rpx";
            }
        }
        .log_button {
            width: 80%;
            margin: 0 auto;
            > view {
                width: 100%;
                height: ~'80rpx';
                background: #f5aa2a;
                margin-top: ~'20rpx';
                border-radius: ~'10rpx';
                line-height: ~'80rpx';
                text-align: center;
                view {
                    display: inline-block;
                    width: ~'100rpx';
                    height: ~'80rpx';
                    line-height: ~'80rpx';
                    color: #fff;
                    .icon-weixin {
                        font-size: ~'60rpx';
                        vertical-align: middle;
                        margin-left: -170rpx;
                    }
                    .icon-iphone {
                        font-size: ~'54rpx';
                        vertical-align: middle;
                    }
                }
                text {
                    color: #fff;
                    letter-spacing: ~'5rpx';
                    vertical-align: middle;
                    font-size: ~'35rpx';
                    padding-left: ~'30rpx';
                }
                #wxLogin{
                    top:-90rpx;
                    background:none;
                    color:#ffffff;
                    padding-left:160rpx;
                    border:none;
                    font-size: 35rpx;
                    letter-spacing:5rpx;
                    line-height: 100rpx;
                }
                #wxLogin::after{
                    content:none !important;
                }
            }
        }
    }
</style>
